<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Frontend\PostController;
use App\Http\Controllers\Frontend\PageController;
use App\Http\Controllers\Frontend\CategoryController;
use App\Http\Controllers\Frontend\TagController;
use App\Http\Controllers\Frontend\AccordionController;

// 首页路由 - 手风琴式布局
Route::get('/', [AccordionController::class, 'index'])->name('home');

// 传统首页路由（备用）
Route::get('/posts-grid', [PostController::class, 'index'])->name('posts.grid');

// 文章路由
Route::prefix('posts')->name('posts.')->group(function () {
    Route::get('/', [PostController::class, 'index'])->name('index');
    Route::get('/{slug}', [PostController::class, 'show'])->name('show');
});

// 分类路由
Route::prefix('categories')->name('categories.')->group(function () {
    Route::get('/', [CategoryController::class, 'index'])->name('index');
    Route::get('/{slug}', [CategoryController::class, 'show'])->name('show');
});

// 标签路由
Route::prefix('tags')->name('tags.')->group(function () {
    Route::get('/', [TagController::class, 'index'])->name('index');
    Route::get('/{slug}', [TagController::class, 'show'])->name('show');
});

// 页面路由
Route::prefix('pages')->name('pages.')->group(function () {
    Route::get('/', [PageController::class, 'index'])->name('index');
    Route::get('/{slug}', [PageController::class, 'show'])->name('show');
});

// 测试路由
Route::get('/test-accordion', function () {
    $categories = \App\Models\Category::withCount(['posts' => function ($query) {
        $query->where('status', 'published')
              ->whereNotNull('published_at')
              ->where('published_at', '<=', now());
    }])
    ->whereNull('parent_id')
    ->orderBy('name')
    ->get()
    ->filter(function ($category) {
        return $category->posts_count > 0;
    });
    
    return view('accordion.index', compact('categories'));
})->name('test.accordion');

// 单独的页面路由（用于自定义页面）
Route::get('/{slug}', [PageController::class, 'show'])->name('page.show')->where('slug', '^(?!admin)[a-zA-Z0-9\-_]+');
